//
//  JHHotUserMoveViewController.m
//  Liberty Voice
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "JHHotUserMoveViewController.h"
#import "JHHotUserMoveTableViewCell.h"
#import "NetwordHandler.h"
#import "JHMoveMOdel.h"
#import "JHUserViewController.h"

@interface JHHotUserMoveViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *hotUSerTable;
@property(nonatomic,retain)UISegmentedControl *hotUserSegmen;

@property(nonatomic,retain)NSMutableArray *HOtUserArr;
@property(nonatomic,retain)NSString *s2;
@end

@implementation JHHotUserMoveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self dian];
            self.HOtUserArr  = [NSMutableArray array];
    self.nextCursor = 1;
    self.s2 = @"hot";
    [self createHotUserSegmented];
    [self createHotUserTableView];
    [self addHeader];
    [self addFooter];
}
- (void)createHotUserSegmented
{
    /**
     :returns: 创建分段控制器
     */
    self.hotUserSegmen = [[UISegmentedControl alloc] initWithItems:@[@"最热",@"最新"]];
    _hotUserSegmen.frame = CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height / 20);
    _hotUserSegmen.selectedSegmentIndex = 0;
    [_hotUserSegmen addTarget:self action:@selector(segmenAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_hotUserSegmen];
    [_hotUserSegmen release];
}
- (void)segmenAction:(UISegmentedControl *)seg
{
    
    //  根据分段标签坐标判断方法
    if (seg.selectedSegmentIndex == 0) {
        NSLog(@"最热");
        self.s2 = @"hot";
        [self addHeader];
        [self addFooter];
    }else
        if (seg.selectedSegmentIndex == 1) {
            NSLog(@"最新");
            self.s2 = @"new";
            [self addHeader];
            [self addFooter];
        }
}
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block JHHotUserMoveViewController *vc = self;
    
    // 添加下拉刷新头部控件
    [self.hotUSerTable addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.isUpLoading = NO;//标记为下拉操作
        [vc GetPersonList:1];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.hotUSerTable headerBeginRefreshing];
    
}
- (void)addFooter
{
    
    //    __unsafe_unretained typeof(self) vc = self;
    __block JHHotUserMoveViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.hotUSerTable addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.isUpLoading = YES;//标记为上拉操作
        [vc GetPersonlist:(vc.nextCursor++) + 1];//请求更多数据
        
    }];
}

- (void)GetPersonList:(NSInteger)nextCursor
{
    /**
     *  获取网络数据
     *
     *  @return 最热
     */
    NSString *s = self.name;
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_list?category_name=%@&condition=%@&device=android&page=%ld&per_page=20", s,_s2,nextCursor];
    
    [NetwordHandler requestWithURL:str completion:^(id result) {
        if (self.isUpLoading == NO) {
            //说明是下拉刷新，就要清空数组中的数据，保持数组中存放的是最新的数据
            [self.HOtUserArr removeAllObjects];
        }
        NSMutableDictionary *dic = result;
        for (NSDictionary *d in [dic objectForKey:@"list"]) {
            JHMoveMOdel *Move = [[JHMoveMOdel alloc] initWithDic:d];
            [_HOtUserArr addObject:Move];
        }
        [self.hotUSerTable headerEndRefreshing];
        [self.hotUSerTable footerEndRefreshing];
        [self.hotUSerTable reloadData];
        
    }];
        [self.indicatorView removeFromSuperview];
}
- (void)GetPersonlist:(NSInteger)nextCursor
{
    /**
     *  获取网络数据
     *
     *  @return 加载
     */
    NSString *s = self.name;
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_list?category_name=%@&condition=%@&device=android&page=%ld&per_page=20", s,_s2,nextCursor];
    NSLog(@"%@", str);
    [NetwordHandler requestWithURL:str completion:^(id result) {

        NSMutableDictionary *dic = result;
        for (NSDictionary *d in [dic objectForKey:@"list"]) {
            JHMoveMOdel *Move = [[JHMoveMOdel alloc] initWithDic:d];
            [_HOtUserArr addObject:Move];
        }

        [self.hotUSerTable footerEndRefreshing];
        [self.hotUSerTable reloadData];
    }];
}

- (void)createHotUserTableView
{
    //  创建tableView
    self.hotUSerTable = [[UITableView alloc] initWithFrame:CGRectMake(0,_hotUserSegmen.frame.size.height + 20, self.view.frame.size.width, self.view.frame.size.height - 40 - 84) style:UITableViewStylePlain];
    _hotUSerTable.delegate = self;
    _hotUSerTable.dataSource= self;
    [_hotUSerTable registerClass:[JHHotUserMoveTableViewCell class] forCellReuseIdentifier:@"Hot"];
    [self.view addSubview:_hotUSerTable];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  重用池
    JHHotUserMoveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Hot"];
    //    cell.textLabel.text = @"dd";
    cell.move = _HOtUserArr[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //  行数
    return _HOtUserArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  行高
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  点击方法
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    JHUserViewController *u = [[JHUserViewController alloc] init];
    u.str = [_HOtUserArr[indexPath.row]uid];
    [self.navigationController pushViewController:u animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
